package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class JdbcUpdate {
    //修改课程信息,根据课程号其他信息
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = "jdbc:mysql://localhost:3306/java76?useSSL=false";
        Connection connection = null;
        Statement st = null;
        try {
            connection = DriverManager.getConnection(url, "root", "root1234");
            System.out.println(connection.getClass().getName());
            Scanner input=new Scanner(System.in);
           System.out.println("请输入课程号：");
            long id=input.nextLong();
            System.out.println("请输入课程名：");
            input.nextLine();
            String  sname=input.nextLine();

            System.out.println("请输入课程学时：");
            int classhour=input.nextInt();
            System.out.println("请输入年级号：");
            long gid=input.nextLong();
            String sql="update s_subject set subjectname='"+sname+"' ,classhour="+classhour+",gradeid="+gid+" where id="+id;
            System.out.println(sql);
            st = connection.createStatement();
            int num = st.executeUpdate(sql);
            System.out.printf("%d行被修改了",num);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                st.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
